<?php
$currencyMark = text::get('currency.mark-' . ViregWizardData::$CURRENCY_CODE);

echo '<h2>' . text::getEscaped('viregWizard.meals') . '</h2>';

// Meal selection

echo '<ul class="' . ($viSeminar['meals_optional'] ? '' : 'hidden') . '">';
$lastMealCode = $viSeminar['end_date'] . '_' . $viSeminar['last_meal'];
echo '<li>';
if (! $displayOnly) {
    echo text::getEscaped('viregWizard.chooseMeals') . ':<br/>';
}

echo '<div style="display: inline-block">';
echo '<table class="mealTable">';
echo '<thead>';

// Days
echo '<tr>';
for ($dayIndex = 0; $dayIndex < $viSeminar['dayCount']; $dayIndex++) {
	$date = date::addDays($viSeminar['begin_date'], $dayIndex);
    echo '<th colspan="3" ';
    if ($dayIndex < $viSeminar['dayCount'] -1) {
        echo 'class="daySeparator"';
    }
    echo ' title="' . format::date($date) . '">' . text::getEscaped('application.dayOfWeek-' . $date->format('N')) . '</th>';
}
echo '</tr>';

// Meal names
echo '<tr class="mealNames">';
for ($dayIndex = 0; $dayIndex < $viSeminar['dayCount']; $dayIndex++) {
	$date = format::isoDate(date::addDays($viSeminar['begin_date'], $dayIndex));
    foreach (registrationHelper::$MEAL_LIST as $meal) {
        if (registrationHelper::mealTableEmptyField($viSeminar, $dayIndex, $meal)) {
            echo '<td class="empty"></td>';
            continue;
        }
        $mealName = text::get('viSeminar.' . $meal);
        echo '<th ';
        if ($meal == 'dinner') {
            echo 'class="daySeparator"';
        }
        $forId = $date . '_' . $meal;
        echo ' title="' . $mealName . '"><label for="' . $forId . '">' . substr($mealName, 0, 1) . '</label></th>';
    }
}
echo '</tr>';

echo '</thead>';
echo '<tbody>';

// Checkboxes
echo '<tr>';
for ($dayIndex = 0; $dayIndex < $viSeminar['dayCount']; $dayIndex++) {
	$date = format::isoDate(date::addDays($viSeminar['begin_date'], $dayIndex));
    foreach (registrationHelper::$MEAL_LIST as $meal) {
        if (registrationHelper::mealTableEmptyField($viSeminar, $dayIndex, $meal)) {
            echo '<td class="empty"></th>';
            continue;
        }

        echo '<td ';
        if ($meal == 'dinner') {
            echo 'class="daySeparator"';
        }
        echo '>';

        $valueAndId = $date . '_' . $meal;
        $extra = 'onchange="' . $jsSubmitForm . '"';
        if ($displayOnly) {
            $extra .= ' disabled="disabled"';
        }
        echo form::checkboxWithId('mealSelection[]', $valueAndId, $valueAndId, $optionsForm, $extra);
        echo '</td>';
    }
}
echo '</tr>';

echo '</tbody>';
echo '</table>';
echo '</div>';

echo '<ul class="mealPriceList">';
foreach (registrationHelper::$MEAL_LIST as $meal) {
    echo '<li>' . text::getEscaped('viSeminar.' . $meal . '_price') . ': ' . format::decimal($viSeminar['prices-' . ViregWizardData::$CURRENCY_CODE][$meal . '_price']) . ' ' . $currencyMark . '</li>';
}
echo '</ul>';

$mealPriceHtml = '<p>' . text::get('viRegistration.meals_price') . ': <span class="registrationPrice">' . format::decimal($optionsForm['meals_price']) . ' ' . $currencyMark . '</span></p>';
echo $mealPriceHtml;

echo '</li>';
echo '</ul>';

if (! $viSeminar['meals_optional']) {
    echo $mealPriceHtml;
}

// Last meal in package option
if ($viSeminar['last_meal_in_package_option']) {
    echo '<ul ';
    if (! is_array($optionsForm['mealSelection']) || array_search($lastMealCode, $optionsForm['mealSelection']) === FALSE) {
        echo 'class="hidden"';
    }
    echo '>';
    echo '<li>';
    if (! $renderedInAdminSpace && ! $displayOnly) {
        echo '<p>' . text::getEscaped('viregWizard.lastMealInPackage') . '</p>';
    }
    $extra = NULL;
    if ($displayOnly) {
        $extra = 'disabled="disabled"';
    }
    echo '<label>' . text::getEscaped('viregWizard.lastMealInPackageOption') . form::yesNoComboBox('last_meal_in_package', $optionsForm['last_meal_in_package'], $extra) . '</label>';
    echo '</li>';
    echo '</ul>';
}
